Page({
  data: {
    id: '',
    detail: {},
    swiperList: [],
    user: {},
    storeupFlag: 0,
    thumbsupFlag: 0,
    crazilyFlag: 0,
    baseUrl: '',
    
    // 砍价相关数据
    kanjiaModalShow: false,
    referencePrice: '',
    kanjiaPrice: ''
  },

  onLoad: function(options) {
    this.setData({
      id: options.id,
      baseUrl: getApp().globalData.baseUrl
    });
  },

  onShow: function() {
    this.init();
    this.getUserInfo();
    this.getStoreup();
    this.getThumbsup();
  },

  // 初始化商品详情
  init: function() {
    const that = this;
    wx.request({
      url: this.data.baseUrl + '/ershoushangpin/info/' + this.data.id,
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code === 0) {
          const detail = res.data.data;
          that.setData({
            detail: detail,
            swiperList: detail.tupian ? detail.tupian.split(',') : []
          });
        }
      }
    });
  },

  // 获取用户信息
  getUserInfo: function() {
    const that = this;
    const tableName = wx.getStorageSync('tableName');
    if (tableName) {
      wx.request({
        url: this.data.baseUrl + '/session/' + tableName,
        method: 'GET',
        header: {
          'token': wx.getStorageSync('token')
        },
        success: function(res) {
          if (res.data.code === 0) {
            that.setData({
              user: res.data.data
            });
          }
        }
      });
    }
  },

  // 砍价按钮点击事件
  onKanjiaTap: function() {
    // 检查是否登录
    if (!this.data.user.id) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    // 检查是否是自己的商品
    if (this.data.user.username === this.data.detail.chiwuzhanghao) {
      wx.showToast({
        title: '不能对自己的商品砍价',
        icon: 'none'
      });
      return;
    }

    this.getReferencePrice();
  },

  // 获取参考价格
  getReferencePrice: function() {
    const that = this;
    wx.request({
      url: this.data.baseUrl + '/kanjia/getReferencePrice',
      method: 'GET',
      data: {
        shangpinId: this.data.id
      },
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code === 0) {
          that.setData({
            referencePrice: res.data.referencePrice,
            kanjiaModalShow: true
          });
        } else {
          wx.showToast({
            title: res.data.msg || '获取参考价格失败',
            icon: 'none'
          });
        }
      }
    });
  },

  // 砍价价格输入
  onKanjiaPriceInput: function(e) {
    this.setData({
      kanjiaPrice: e.detail.value
    });
  },

  // 关闭砍价弹窗
  closeKanjiaModal: function() {
    this.setData({
      kanjiaModalShow: false,
      kanjiaPrice: ''
    });
  },

  // 提交砍价
  submitKanjia: function() {
    const kanjiajia = parseFloat(this.data.kanjiaPrice);
    
    if (!kanjiajia || kanjiajia <= 0) {
      wx.showToast({
        title: '请输入有效的砍价金额',
        icon: 'none'
      });
      return;
    }

    if (kanjiajia >= this.data.detail.price) {
      wx.showToast({
        title: '砍价金额不能大于等于原价',
        icon: 'none'
      });
      return;
    }

    const that = this;
    wx.request({
      url: this.data.baseUrl + '/kanjia/save',
      method: 'POST',
      data: {
        shangpinid: this.data.id,
        kanjiajia: kanjiajia
      },
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code === 0) {
          wx.showToast({
            title: '砍价申请提交成功',
            icon: 'success'
          });
          that.closeKanjiaModal();
        } else {
          wx.showToast({
            title: res.data.msg || '提交失败',
            icon: 'none'
          });
        }
      }
    });
  },

  // 收藏功能
  shoucang: function() {
    if (!this.data.user.id) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    const that = this;
    if (this.data.storeupFlag === 0) {
      // 收藏
      wx.request({
        url: this.data.baseUrl + '/storeup/save',
        method: 'POST',
        data: {
          refid: this.data.id,
          tablename: 'ershoushangpin',
          name: this.data.detail.shangpinmingcheng,
          picture: this.data.swiperList[0] || '',
          type: 1
        },
        header: {
          'Content-Type': 'application/json',
          'token': wx.getStorageSync('token')
        },
        success: function(res) {
          if (res.data.code === 0) {
            wx.showToast({
              title: '收藏成功',
              icon: 'success'
            });
            that.getStoreup();
          }
        }
      });
    } else {
      // 取消收藏
      wx.showModal({
        title: '提示',
        content: '是否取消收藏',
        success: function(res) {
          if (res.confirm) {
            that.cancelStoreup();
          }
        }
      });
    }
  },

  // 取消收藏
  cancelStoreup: function() {
    // 实现取消收藏逻辑
  },

  // 获取收藏状态
  getStoreup: function() {
    const that = this;
    wx.request({
      url: this.data.baseUrl + '/storeup/list',
      method: 'GET',
      data: {
        page: 1,
        limit: 1,
        refid: this.data.id,
        tablename: 'ershoushangpin',
        type: 1
      },
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code === 0) {
          that.setData({
            storeupFlag: res.data.data.list.length > 0 ? 1 : 0
          });
        }
      }
    });
  },

  // 获取点赞状态
  getThumbsup: function() {
    const that = this;
    wx.request({
      url: this.data.baseUrl + '/storeup/list',
      method: 'GET',
      data: {
        page: 1,
        limit: 1,
        refid: this.data.id,
        tablename: 'ershoushangpin',
        type: 21
      },
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code === 0) {
          that.setData({
            thumbsupFlag: res.data.data.list.length > 0
          });
        }
      }
    });
  },

  // 点赞
  zan: function() {
    // 实现点赞逻辑
  },

  // 点踩
  cai: function() {
    // 实现点踩逻辑
  },

  // 加入购物车
  onCartTap: function() {
    if (!this.data.user.id) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    const that = this;
    wx.request({
      url: this.data.baseUrl + '/cart/save',
      method: 'POST',
      data: {
        tablename: 'ershoushangpin',
        goodid: this.data.id,
        goodname: this.data.detail.shangpinmingcheng,
        chiwuzhanghao: this.data.detail.chiwuzhanghao,
        picture: this.data.swiperList[0] || '',
        buynumber: 1,
        price: this.data.detail.price
      },
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code === 0) {
          wx.showToast({
            title: '添加到购物车成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: res.data.msg || '添加失败',
            icon: 'none'
          });
        }
      }
    });
  },

  // 立即购买
  onBuyTap: function() {
    if (!this.data.user.id) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    const orderGoods = [{
      tablename: 'ershoushangpin',
      goodid: this.data.id,
      goodname: this.data.detail.shangpinmingcheng,
      chiwuzhanghao: this.data.detail.chiwuzhanghao,
      picture: this.data.swiperList[0] || '',
      buynumber: 1,
      price: this.data.detail.price
    }];

    wx.setStorageSync('orderGoods', orderGoods);
    wx.navigateTo({
      url: '../shop-order-confirm/shop-order-confirm?type=1'
    });
  }
}); 